import { putMyInfo } from '@/api/user'
export default {
  state: () => {
    return {
      userInfo: null
    }
  },
  getters: {
    /**
     * 获取我的信息
     * @param {String} key 指定字段
     */
    getMyInfo(state) {
      return JSON.parse(JSON.stringify(state.userInfo))
    }
  },
  mutations: {
    /**
     * 设置用户信息
     * @param {Object} userInfo
     */
    setUserInfo(state, userInfo) {
      state.userInfo = {
        ...state.userInfo,
        ...userInfo
      }
      /* 修改头像 */
      if (userInfo.role) {
        const avatars = {
          parent: {
            avatar: 'https://aha-public-1301319986.cos.ap-shanghai.myqcloud.com/static_file/yuping/userAvatar/20220308094833150-Ellipse 13.png',
            avatarLarge: 'https://aha-public-1301319986.cos.ap-shanghai.myqcloud.com/static_file/yuping/userAvatar/20220312153807850-Ellipse 13 (3).png',
          },
          younger: {
            avatar: 'https://aha-public-1301319986.cos.ap-shanghai.myqcloud.com/static_file/yuping/userAvatar/20220308094833149-Ellipse 13 (1).png',
            avatarLarge: 'https://aha-public-1301319986.cos.ap-shanghai.myqcloud.com/static_file/yuping/userAvatar/20220312162743943-Ellipse 14 (1).png',
          },
          child: {
            avatar: 'https://aha-public-1301319986.cos.ap-shanghai.myqcloud.com/static_file/yuping/userAvatar/20220312155002186-Ellipse 15.png',
            avatarLarge: 'https://aha-public-1301319986.cos.ap-shanghai.myqcloud.com/static_file/yuping/userAvatar/20220312155002186-Ellipse 15.png',
          }
        }

        state.userInfo = {
          ...state.userInfo,
          avatar: avatars[ userInfo.role ].avatar,
          avatarLarge: avatars[ userInfo.role ].avatarLarge,
        }
      }

      putMyInfo(state.userInfo)
      console.log(state.userInfo)
    },
  }
}
